package com.yonyou.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.JdkIdGenerator;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.MateData;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.yonyou.dao.PrizeDAO;
import com.yonyou.domain.DrawTurn;
import com.yonyou.domain.Prize;
import com.yonyou.domain.ResponseEntity;
import com.yonyou.status.CommnonStatus;
import com.yonyou.util.TimeUtil;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = "/prize")
public class PrizeController {

	@Autowired
	private PrizeDAO prizeDAO;
	
	@Autowired
    private FastFileStorageClient storageClient;
	
	/**
	 * 获取奖项集合
	 * @param meeting_id
	 * @return
	 */
	@RequestMapping(value="/list/{meeting_id}",method = RequestMethod.GET)
	@ApiOperation(value = "获取奖项集合",httpMethod ="GET", response = ResponseEntity.class,notes = "获取奖项集合")
	public ResponseEntity<List<Prize>> list(@ApiParam(required = true, name = "paramData", value = "会议ID") @PathVariable String meeting_id) {
		ResponseEntity<List<Prize>> responseEntity = new ResponseEntity<List<Prize>>();
		try {
			responseEntity.setData(prizeDAO.list(meeting_id));
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
		}
		return responseEntity;
	}

	/**
	 * 保存奖项信息
	 * 
	 * @return
	 */
	@RequestMapping(value="/save",method =RequestMethod.POST)
	@ApiOperation(value = "保存奖项信息",httpMethod ="POST", response = ResponseEntity.class,notes = "保存奖项信息")
	public ResponseEntity<Prize> save(@ApiParam(required = true, name = "paramData", value = "奖项信息") @RequestBody Prize prize) {
		ResponseEntity<Prize> responseEntity = new ResponseEntity<Prize>();
		if(!"".equals(checkSave(prize))){
			 responseEntity.setCode(1);
			 responseEntity.setErrmsg(checkSave(prize));
			 return responseEntity;
		}
		try {
			List<DrawTurn> list = prize.getList();
			prize.setId(new JdkIdGenerator().generateId().toString());
			if(CommnonStatus.AWARDSTATUS.equals( prize.getLottery_allocation())){
					list =new ArrayList<DrawTurn>();
					for (int i = 0; i < prize.getWinning_quota()/prize.getOnetime_extraction(); i++) {
						DrawTurn drawTurn = new DrawTurn();
						drawTurn.setPrize_id(prize.getId());
						drawTurn.setId(new JdkIdGenerator().generateId().toString());
						drawTurn.setTurn(i);
						drawTurn.setDraw_form(prize.getDraw_form());
						drawTurn.setWinning_quota(prize.getOnetime_extraction());
						list.add(drawTurn);
					}
			}else{
				for (Iterator<DrawTurn> iterator = list.iterator(); iterator.hasNext();) {
					DrawTurn drawTurn = (DrawTurn) iterator.next();
					drawTurn.setId(new JdkIdGenerator().generateId().toString());
				}
			}
			prize.setList(list);
			prize.setDr(CommnonStatus.SAVSTATUS);
			prize.setTs(TimeUtil.getCurrentTime());
			prizeDAO.save(prize);
			responseEntity.setData(prize);
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
		}
		return responseEntity;

	}
	/**
	 * 校验保存方法
	 * @param responseEntity
	 * @param prize
	 * @return
	 */
	private String checkSave(Prize prize){
		String errmsg = "";
		if(prize.getMeeting_id()==null||"".equals(prize.getMeeting_id())){
			errmsg = "会议不能为空!";
		}
		if(prize.getPrize_name()==null||"".equals(prize.getPrize_name())){
			errmsg = "奖项不能为空!";
		}
		if(prize.getPrizes()==null||"".equals(prize.getPrizes())){
			errmsg = "奖品不能为空!";
		}
		if(prize.getIs_allowed_duplicate()==null||"".equals(prize.getIs_allowed_duplicate())){
			errmsg = "是否允许重复不能为空!";
		}
		if(prize.getDraw_mode()==null||"".equals(prize.getDraw_mode())){
			errmsg = "抽奖模式不能为空!";
		}
		return errmsg;
	}
	/**
	 * 修改奖项信息
	 * 
	 * @return
	 */
	@RequestMapping(value="/update",method = RequestMethod.POST)
	@ApiOperation(value = "修改奖项信息",httpMethod ="POST", response = ResponseEntity.class,notes = "修改奖项信息")
	public ResponseEntity<Prize> update(@ApiParam(required = true, name = "paramData", value = "奖项信息,id不能为空") @RequestBody Prize prize) {
		ResponseEntity<Prize> responseEntity = new ResponseEntity<Prize>();
		if(!"".equals(checkSave(prize))){
			 responseEntity.setCode(1);
			 responseEntity.setErrmsg(checkSave(prize));
			 return responseEntity;
		}
		try {
			List<DrawTurn> list = prize.getList();
			if(CommnonStatus.AWARDSTATUS.equals( prize.getLottery_allocation())){
					list =new ArrayList<DrawTurn>();
					for (int i = 0; i < prize.getWinning_quota()/prize.getOnetime_extraction(); i++) {
						DrawTurn drawTurn = new DrawTurn();
						drawTurn.setPrize_id(prize.getId());
						drawTurn.setId(new JdkIdGenerator().generateId().toString());
						drawTurn.setTurn(i);
						drawTurn.setDraw_form(prize.getDraw_form());
						drawTurn.setWinning_quota(prize.getOnetime_extraction());
						list.add(drawTurn);
					}
			}else{
				for (Iterator<DrawTurn> iterator = list.iterator(); iterator.hasNext();) {
					DrawTurn drawTurn = (DrawTurn) iterator.next();
					drawTurn.setId(new JdkIdGenerator().generateId().toString());
				}
			}
			prize.setList(list);
			prize.setDr(CommnonStatus.SAVSTATUS);
			prize.setTs(TimeUtil.getCurrentTime());
			prizeDAO.update(prize);
			responseEntity.setData(prize);
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
		}
		return responseEntity;
	}

	/**
	 * 删除奖项信息
	 * 
	 * @return
	 */
	@RequestMapping(value="/delete",method = RequestMethod.POST)
	@ApiOperation(value = "删除奖项信息",httpMethod ="POST", response = ResponseEntity.class,notes = "删除奖项信息")
	public ResponseEntity<Prize> delete(@ApiParam(required = true, name = "paramData", value = "奖项信息,只传ID即可") @RequestBody Prize prize) {
		ResponseEntity<Prize> responseEntity = new ResponseEntity<Prize>();
		try {
			prize.setTs(TimeUtil.getCurrentTime());
			prizeDAO.delete(prize);
			responseEntity.setData(prize);
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
		}
		return responseEntity;
	}

	/**
	 * 上传图片
	 * @param prize
	 * @return
	 */
	@RequestMapping(value="/savefile",method = RequestMethod.POST)
	@ApiOperation(value = "上传图片文件",httpMethod ="POST", response = ResponseEntity.class,notes = "上传图片文件")
	public ResponseEntity<String> saveFile(@RequestParam(value = "file", required = false)MultipartFile file) {
		ResponseEntity<String> responseEntity = new ResponseEntity<String>();
		try {
			Set<MateData> metedatas=new HashSet<>();
			MateData  info=new MateData();
			info.setName("抽奖奖项");
			info.setValue("奖品图片");
			metedatas.add(info);
			StorePath fileinfo=storageClient.uploadFile(file.getInputStream(), file.getBytes().length, "jpg", metedatas);
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
			responseEntity.setData(fileinfo.getFullPath());
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常,图片上传失败");
		}
		return responseEntity;
	}
}
